Thread: [Linked Lists] Custom String Class Add Char problems

  1. #1
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986

    Unhappy [Linked Lists] Custom String Class Add Char problems

    Hi guys,

    It's been a while since I did anything with C++, but I am attempting to get back into it for the writing of my own little operating system. As I can't use any external headers or libraries, I have decided to create my own set of libraries based loosely on the .NET framework.

    I have started by creating a simple string class using linked lists. But it's not working . It's been a while since I used pointers so that's probably where the problem is.

    I'll post the code that I think is causing the problem below, and I'll also upload the VC++ 6 project and source. If anyone could take a look and tell me where I've gone wrong, I would really appreciate it.

    The string code is broken into two classes - String and StringNode. String contains a member called "FirstNode", which points to the first StringNode in the list (the first character).

    The problem seems to be that FirstNode is not being assigned by my Add(char) function.

    Main() - DebugMain.cpp
    PHP Code:
    #include "System.String.hpp"
    #include <iostream>

    using namespace std;
    using namespace System;

    int main()
    {
        
    String = new String("Hello");
        *
    "Yellow";
        
    s->Traverse();
        
    cout << s->ToChar() << endl;
        
    delete s;
        return 
    0;

    System.String.Hpp - String Header File
    PHP Code:
    #ifndef SYSTEM_STRING_H
    #define SYSTEM_STRING_H 1

    namespace System
    {
        class 
    String
        
    {
            public:
                
    String();
                
    String(const char *);
                const 
    char operator[](const int Index);
                
    String &operator+=(const char &);
                
    String &operator=(const char chararray);
                
    bool Add(const char C);
                
    bool Add(String);
                
    bool Clear();
                const 
    char ToChar();
                ~
    String();
                
    void Traverse();
            private:
                class 
    StringNode
                
    {
                    public:
                        
    StringNode();
                        
    StringNode(const char wC);
                        
    char C;
                        
    StringNode Next;
                };

                
    StringNode FirstNode;
        };
    }

    #endif 
    Add(char) Function
    PHP Code:
    //-----------------------------------------------------------------------------
    /// <summary>
    ///    Adds a character to the String.
    /// </summary>
    /// <param name="C">The character to add.</param>
    /// <returns>True if the character was added.</returns>
    //-----------------------------------------------------------------------------
    bool System::String::Add(const char C)
    {
        
    cout << "Add(" << << ") called." << endl;
        
    StringNode node FirstNode;
        
    cout << "&*node: " << (&*node) << endl;
        while (
    node != 0)
        {
            
    node node->Next;
        }

        
    node = new StringNode(C);
        
    cout << "&*node: " << (&*node) << endl;
        
    cout << "&*FirstNode: " << (&*FirstNode) << endl;
        return 
    true;

    String Constructor
    PHP Code:
    System::String::String(const char Text)
    {
        
    this->FirstNode 0;
        
    int X 0;
        while (
    Text[X] != && Text[X] != '\0')
        {
            
    this->Add(Text[X]);
            
    X++;
        }

    When run, FirstNode has the address of 0 even after Add() has been called. At the start of Add(), doesn't my logic mean a new StringNode will be created and assigned where FirstNode is?

    Thank you to anyone who can help!

    Paul Stovell
    Last edited by nickname_changed; 07-23-2004 at 07:27 PM.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    the problem is, you traversed the list to far: remember - you're adding to the list - you don't want 'node' to be NULL!

    Code:
    while (node->Next != 0) 
        { 
            node = node->Next; 
        } 
    
        node->Next = new StringNode(C);
    also be sure that StringNode's constructor initializes it's 'Next' ponter to NULL.

    >> cout << "&*node: " << (&*node) << endl;

    that's the very same as:

    cout << "node: " << node << endl;

    if you're trying to print the address of the pointer on the other hand, it would be:

    cout << "&node: " << &node << endl;
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. Problems with my custom string class
    By cunnus88 in forum C++ Programming
    Replies: 15
    Last Post: 11-15-2005, 08:21 PM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM